import time
from signal import pause

from gpiozero import Button

from library import respberrypi

IN1 = 0
IN2 = 5
IN3 = 6
IN4 = 13

STEPS = [IN1, IN2, IN3, IN4]

BUTTON_PIN = 4


def send_step(i):
    for length in range(len(STEPS)):
        if length == i:
            respberrypi.gpio_output(STEPS[length], respberrypi.SIG_HIGH)
        else:
            respberrypi.gpio_output(STEPS[length], respberrypi.SIG_LOW)
    time.sleep(0.004)


def start_stepper(times=100):
    for t in range(times):
        for i in range(4):
            send_step(i)


def stop_stepper():
    for s in STEPS:
        respberrypi.gpio_output(s, respberrypi.SIG_LOW)


if __name__ == '__main__':
    out_channels = []
    for step in STEPS:
        out_channels.append([step, respberrypi.SIG_LOW])
    respberrypi.gpio_init(out_channels=out_channels)
    button = Button(BUTTON_PIN, hold_time=1)
    button.when_held = start_stepper
    button.when_released = stop_stepper
    pause()
